using System;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using SimpleJSON;
using UnityEngine;
using Random = UnityEngine.Random;

namespace Gj
{
    public static class StringTools
    {
        public static float GetStringWidthByFont(Font font, string str)
        {
            float width = 0;
            CharacterInfo charInfo;
            for (var i = 0; i < str.Length; i++)
            {
                font.GetCharacterInfo(str[i], out charInfo);
                width += charInfo.advance;
            }

            return width;
        }

        public static JSONNode GetNode(JSONNode node, string[] keys)
        {
            if (node == null) return null;
            for (var i = 0; i < keys.Length; i++)
            {
                var rgx = new Regex(@"\[(\d+)\]");
                var tmp = rgx.Match(keys[i]).Value;
                if (!IsEmpty(tmp))
                {
                    tmp = tmp.Substring(1, tmp.Length - 2);
                    try
                    {
                        var index = int.Parse(tmp);
                        node = node[index];
                    }
                    catch (Exception e)
                    {
                        return null;
                    }
                }
                else
                {
                    if (node[keys[i]].IsNull) return null;
                    node = node[keys[i]];
                }
            }

            return node;
        }
        
        public static string GenerateRandom(int length)
        {
            // 数字
            char[] number = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
            // 字母
            char[] letter = {                                
                                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
                                'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
                                'u', 'v', 'w', 'x', 'y', 'z',
                                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
                                'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
                                'U', 'V', 'W', 'X', 'Y', 'Z'                              
                            };
            // 特殊符号
            char[] symbol = { '!', '@', '#', '$', '%', '&', '*', '.', '_' };
            string result = "";

            if (length >= 6)
            {
                for (int i = 0; i < length; i++)
                {
                    if (i == 2)
                    {
                        result += symbol[Random.Range(0, symbol.Length)];
                    }
                    else
                    {
                        result += i % 2 == 0 ? number[Random.Range(0, number.Length)] : letter[Random.Range(0, letter.Length)];
                    }
                }
            }
            return result;
        }

        public static JSONArray ToArray(object[] objs)
        {
            var arr = new JSONArray();
            if (objs == null) return arr;
            foreach (var obj in objs)
            {
                if (obj == null) continue;
                arr.Add(obj.ToString());
            }

            return arr;
        }

        public static float ToFloat(string s)
        {
            return float.Parse(s, CultureInfo.InvariantCulture.NumberFormat);
        }

        public static int ToInt(string s)
        {
            return int.Parse(s, CultureInfo.InvariantCulture.NumberFormat);
        }

        public static double ToDouble(string s)
        {
            return double.Parse(s, CultureInfo.InvariantCulture.NumberFormat);
        }

        public static bool IsEmpty(string s)
        {
            return s == null || s == "";
        }

        public static bool IsNum(string s)
        {
            return s != null && s != "";
        }

        public static string[] StringToList(string str)
        {
            return str.Split('*');
        }

        public static string Md5(string str)
        {
            var pwd = "";
            var md5 = MD5.Create();
            var s = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
            for (var i = 0; i < s.Length; i++) pwd = pwd + s[i];
            return pwd;
        }

        public static string GetSize(float size)
        {
            var t = "MB";
            size = size / 1024;
            if (size > 1000)
            {
                size = size / 1024;
                t = "GB";
            }

            return string.Format("{0:F}{1}", size, t);
        }

        public static string GenerateStr(string[] chars, int Length)
        {
            var count = chars.Length;
            var newRandom = new StringBuilder(count);

            for (var i = 0; i < Length; i++) newRandom.Append(chars[Random.Range(0, count)]);
            return newRandom.ToString();
        }

        public static string GenerateNum(int Length)
        {
            char[] chars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

            var newRandom = new StringBuilder(10);
            for (var i = 0; i < Length; i++) newRandom.Append(chars[Random.Range(0, 10)]);
            return newRandom.ToString();
        }

        public static string GenerateStr(int Length)
        {
            char[] chars =
            {
                '0',
                '1',
                '2',
                '3',
                '4',
                '5',
                '6',
                '7',
                '8',
                '9',
                'a',
                'b',
                'c',
                'd',
                'e',
                'f',
                'g',
                'h',
                'i',
                'j',
                'k',
                'l',
                'm',
                'n',
                'o',
                'p',
                'q',
                'r',
                's',
                't',
                'u',
                'v',
                'w',
                'x',
                'y',
                'z',
                'A',
                'B',
                'C',
                'D',
                'E',
                'F',
                'G',
                'H',
                'I',
                'J',
                'K',
                'L',
                'M',
                'N',
                'O',
                'P',
                'Q',
                'R',
                'S',
                'T',
                'U',
                'V',
                'W',
                'X',
                'Y',
                'Z'
            };

            var newRandom = new StringBuilder(62);
            for (var i = 0; i < Length; i++) newRandom.Append(chars[Random.Range(0, 62)]);
            return newRandom.ToString();
        }
    }
}